package org.dallasrowing.logbook.ui.controller;

import java.util.Date;
import java.util.List;

import org.dallasrowing.logbook.ui.view.LogBookWindow;
import org.dallasrowing.logbook.ui.view.NewEntryWindow;
import org.dallasrowing.model.Boat;
import org.dallasrowing.model.LogBookEntry;
import org.dallasrowing.model.Rower;
import org.dallasrowing.model.dao.BoatsDAO;
import org.dallasrowing.model.dao.DAOException;
import org.dallasrowing.model.dao.LogBookDAO;
import org.dallasrowing.model.dao.RowersDAO;

public class NewLogEntryControllerImpl implements NewLogEntryController {

	private BoatsDAO boatsDAO;
	private LogBookWindow mainWindow;
	private NewEntryWindow newEntryWindow;
	private RowersDAO rowersDAO;
	private LogBookDAO logBookDAO;

	public NewLogEntryControllerImpl(LogBookWindow mainWindow, BoatsDAO boatsDAO, RowersDAO rowersDAO, LogBookDAO logBookDAO)
	{
		this.boatsDAO = boatsDAO;
		this.rowersDAO = rowersDAO;
		this.logBookDAO = logBookDAO;
		this.mainWindow = mainWindow;
	}
	
	private void registerNewEntryWindowListeners(final LogBookUiController parentController) {
		newEntryWindow.setSubmitButtonListener(new Runnable() {
		
			public void run() {
				submitNewLogBookEntry();
			}
			
		});
		
		newEntryWindow.setBoatSelectedListener(new Runnable(){
		
			public void run() {
				boatSelected();
			}
		
		});
		
		newEntryWindow.setRowerMissingButtonListener(new Runnable(){
			public void run() {
				rowerMissingButtonClicked();
			}
		});
	}

	protected void rowerMissingButtonClicked() {
		RowerMissingController controller = new RowerMissingControllerImpl(mainWindow, this, rowersDAO);
		controller.newNewRowerWindow(newEntryWindow.openNewRowerWindow());
		controller.openNewRowerWindow();
	}


	protected void boatSelected() {
		Boat selectedBoat = newEntryWindow.getSelectedBoat();
		if (selectedBoat == null)
		{
			throw new RuntimeException("boat not found");
		}
		updateRowerFields(selectedBoat.getSeats(), selectedBoat.isWithCox());
	}

	private void updateRowerFields(int seats, boolean withCox) {
		try {
			List<Rower> allRowers = rowersDAO.searchRowersByName("", true);
			newEntryWindow.updateParticipantFields(seats, withCox, allRowers);
		} catch (DAOException e) {
			mainWindow.showError(e);
		}
	}

	protected void submitNewLogBookEntry() {
		Date startDate = newEntryWindow.getStartDate();
		Boat selectedBoat = newEntryWindow.getSelectedBoat();
		List<Rower> selectedRowers = newEntryWindow.getSelectedRowers();
		
		try {
		
			LogBookEntry newLogBookEntry = new LogBookEntry(startDate, selectedBoat, selectedRowers);
			logBookDAO.create(newLogBookEntry);
			
			newEntryWindow.closeWindow();

		} catch (Exception e) {
			mainWindow.showError(e);
		}
	}

	public void newLogEntryWindow(NewEntryWindow newEntryWindow) {
		this.newEntryWindow = newEntryWindow;
	}

	public void openNewEntryWindow(LogBookUiController parentController) {
		populateWindowWithData();
		registerNewEntryWindowListeners(parentController);
	}

	private void populateWindowWithData() {
		newEntryWindow.setNewEntryStartDate(new Date());
		try {
			newEntryWindow.updateBoatsList(boatsDAO.allBoats(false));
		} catch (DAOException e) {
			mainWindow.showError(e);
		}
	}

	public void refreshRowers() {
		try {
			boatSelected();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}
	
}
